package cat.joronya.supermanager.service;

import android.util.Log;
import cat.joronya.supermanager.Supermanager;

public class MarketParserProvider 
{
	private static Class parsers[] = {
		cat.joronya.supermanager.service.htmlcleanerImpl.MarketParserImpl.class, 
		cat.joronya.supermanager.service.substrImpl.MarketParserImpl.class
	};

	// return the first registered parser
	public static MarketParser getDefault() 
	{
		try {
			for (Class parser : parsers) 
			{
				//We are only expecting one
				Log.d(Supermanager.TAG, "Loaded market parser impl: "+parser.getCanonicalName());
				MarketParser lp;

				lp = (MarketParser)parser.newInstance();
				return lp;
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		throw new Error ("No MarketParser registered");
	}

	public static MarketParser getImpl(Package packageName)
	{
		try {
			for (Class parser : parsers) 
			{
				// check package name
				if( !parser.getPackage().equals(packageName) )
					continue;

				Log.d(Supermanager.TAG, "Loaded market parser impl: "+parser.getCanonicalName());
				MarketParser lp;

				lp = (MarketParser)parser.newInstance();
				return lp;
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		throw new Error ("No MarketParser registered");
	}
}
